// <editor-fold defaultstate="collapsed" desc="GNU GPLv3 Header">
/* 
 * File:   LCD_TM162JCAWG1.h
 *
 * Copyright (C) 2013 Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * Copyright (C) 2013 David Megias Jimenez <dmegias@uoc.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
// </editor-fold>

#ifndef LCD_TM162JCAWG1_H
#define	LCD_TM162JCAWG1_H

#ifdef	__cplusplus
extern "C" {
#endif

// <editor-fold defaultstate="collapsed" desc="defines">
#ifndef FCY
#define FCY 40000000UL // 40 MIPS
#endif
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="includes">
#include <p33FJ256GP710A.h>
#include <libpic30.h>
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="define dsPIC-LCD pins">
/*
 * dsPIC-LCD pins
 */
#define LCD_RW LATDbits.LATD5
#define LCD_RS LATBbits.LATB15
#define LCD_E LATDbits.LATD4
#define LCD_DATA LATE
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="define dsPIC-LCD pin direction">
/*
 * dsPIC-LCD pin direction (I/O)
 */
#define LCD_RW_TRIS TRISDbits.TRISD5
#define LCD_RS_TRIS TRISBbits.TRISB15
#define LCD_E_TRIS TRISDbits.TRISD4
#define LCD_DATA_TRIS TRISE
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="funtion prototypes">
    void lcd_cmd_short_delay(char cmd);
    void lcd_cmd_long_delay(char cmd);
    void lcd_init(void);
    void lcd_data(char data);
    void lcd_puts(unsigned char *data, unsigned char count);
    void lcd_set_address_CGRAM(unsigned char address);
    void lcd_set_address_DDRAM(unsigned char address);
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="lcd command prototypes">
/*************************************************************
 * LCD COMMAND FUCNTION PROTOTYPES
 *************************************************************/

// <editor-fold defaultstate="collapsed" desc="lcd_home_clr">
// Write "20H" to DDRAM and set DDRAM address to "00H" from AC
#define lcd_home_clr()            lcd_cmd_long_delay( 0x01 ) // 0000:0001
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="lcd_home_it">
// DDRAM address is set to 00h from AC and the cursor returns to 00h position.
// The contents of DDRAM are not changed.
#define lcd_home_it()             lcd_cmd_long_delay( 0x02 ) // 0000:0010
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="lcd_line">
/*
 * Set the cursor at the beginning of the line.
 * The contents of DDRAM are not changed. @DDRAM
 */
#define lcd_line_1()              lcd_cmd_short_delay( 0x80 ) // 1000:0000
#define lcd_line_2()              lcd_cmd_short_delay( 0xC0 ) // 1100:0000
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="lcd_function_set">
/*
 * Set interface data length (DL: 4-bit / 8-bit) instruction
 */
#define lcd_function_set_4bit()   lcd_cmd_short_delay( 0x20 ) // 0010:0000
#define lcd_function_set_8bit()   lcd_cmd_short_delay( 0x3C ) // 0011:0000
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="lcd_cursor_shift">
/*
 * Cursor Shift
 */
#define lcd_cursor_shift_left()   lcd_cmd_short_delay( 0x10 ) // 0001:0000
#define lcd_cursor_shift_right()  lcd_cmd_short_delay( 0x14 ) // 0001:0100
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="lcd_display_shift">
/*
 * Screen Shift
 */
#define lcd_display_shift_left()  lcd_cmd_short_delay( 0x18 ) // 0001:1000
#define lcd_display_shift_right() lcd_cmd_short_delay( 0x1C ) // 0001:1100
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Entry mode set">
/*
 * Entry mode set
 * Set display (D), cursor (C), and blinking of cursor (B) ON / OFF control
 */
#define lcd_ems_dec_shift_off()   lcd_cmd_short_delay( 0x04 ) // 0000:0100
#define lcd_ems_dec_shift_on()    lcd_cmd_short_delay( 0x05 ) // 0000:0101
#define lcd_ems_inc_shift_off()   lcd_cmd_short_delay( 0x06 ) // 0000:0110
#define lcd_ems_inc_shift_on()    lcd_cmd_short_delay( 0x07 ) // 0000:0111
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Display, Cursor, Cursor Blink Config">
/*
 * Display, Cursor, Cursor Blink Config
 * Set display (D), cursor (C), and blinking of cursor (B) ON / OFF control
 */
#define lcd_D_off_C_off_B_off()   lcd_cmd_short_delay( 0x08 ) // 0000:1000
#define lcd_D_off_C_off_B_on()    lcd_cmd_short_delay( 0x09 ) // 0000:1001
#define lcd_D_off_C_on_B_off()    lcd_cmd_short_delay( 0x0A ) // 0000:1010
#define lcd_D_off_C_on_B_on()     lcd_cmd_short_delay( 0x0B ) // 0000:1011
#define lcd_D_on_C_off_B_off()    lcd_cmd_short_delay( 0x0C ) // 0000:1100
#define lcd_D_on_C_off_B_on()     lcd_cmd_short_delay( 0x0D ) // 0000:1101
#define lcd_D_on_C_on_B_off()     lcd_cmd_short_delay( 0x0E ) // 0000:1110
#define lcd_D_on_C_on_B_on()      lcd_cmd_short_delay( 0x0F ) // 0000:1111
// </editor-fold>

// </editor-fold>

#ifdef	__cplusplus
}
#endif

#endif	/* LCD_TM162JCAWG1_H */

